var App = getApp()

const request = (url, data, method = 'POST') => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: url,
      data: Object.assign(data, {
        uid: App.uid(),
        token: App.token()
      }),
      header: {
        'Content-Type': (method == 'POST' ? 'application/x-www-form-urlencoded' : 'application/json')
      },
      method: method,
      success: (r) => {
        if(r.statusCode == 200) {
          resolve(r.data)
        } else {
          reject(new Error(r.statusCode))
        }
      },
      fail: () => {
        reject(new Error(500))
      }
    })
  })
}

export { App, request }
